public class Solution2 {
    int n;
    public int rob(int[] nums) {
        n = nums.length;
        int ret1 = rob(nums, 2, n - 2) + nums[0];
        int ret2 = rob(nums, 1, n - 1);
        return Math.max(ret1, ret2);
    }

    public int rob(int[] nums, int begin, int end) {
        if(begin > end) {
            return 0;
        }
        int[] f = new int[n];
        int[] g = new int[n];
        f[begin] = nums[begin];
        for(int i = begin + 1; i <= end; i++) {
            f[i] = g[i - 1] + nums[i];
            g[i] = Math.max(f[i - 1], g[i - 1]);
        }
        return Math.max(f[end], g[end]);
    }
}
